const Router = require('koa-router')
const Print = require('../schema/Print')

const printRouter = new Router({
  prefix: '/recordApi/print'
})

printRouter.get('/template', async ctx => {
  const query = ctx.query;
  let template;
  switch(query.templateOwnerType) {
    case 'doctor':
      template = await Print.findOne({
        templateOwnerType: 'doctor',
        templateOwner: query.templateOwner
      })
      break
    case 'group':
      template = await Print.findOne({
        templateOwnerType:  'group',
        templateOwner: query.templateOwner
      })
      break
    case 'dept':
      template = await Print.findOne({
        templateOwnerType:  'dept',
        templateOwner: query.templateOwner
      })
      break
    case 'hospital':
      template = await Print.findOne({
        templateOwnerType: 'hospital'
      })
      break
    default:
      template = '参数不对'
  }
  ctx.body = template
})

printRouter.patch('/template', async ctx => {
  let printTemp = ctx.request.body.printTemp
  await Print.findOneAndUpdate({ hospital: 'xxx' }, { $set: { template: printTemp }})
  ctx.body = {
    result: 'success'
  }
})

module.exports = printRouter